package com.nana.petadventure.pe
{
	import org.cove.ape.APEVector;
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.CircleParticle;
	import org.cove.ape.CollisionEvent;
	import org.cove.ape.Group;
	import org.cove.ape.VectorForce;
	
	public class CatGroup extends Group
	{
		public static const CRASH_HORIZONTAL_SPEED:Number = 12.0;
		private var _powered:Boolean;
		private var _catBody:CircleParticle;
		private var _status:int;
		
		// 正常游戏进行中渐渐加速；撞击到障碍物后减速
		private var _catSpeed:Number;
		
		private var _catDisplay:Cat;
		
		public function CatGroup(collideInternal:Boolean=false)
		{
			super(collideInternal);
			
			// 生成CatBody实例
			_catBody = new CircleParticle(120,500,45,false,1.5,0);
			this.addParticle(_catBody);
			_catDisplay = new Cat;
			catBody.setDisplay(_catDisplay,-45,-45);
			catBody.sprite.cacheAsBitmap = true;
			
			catSpeed = 20;
		}
		
		// 由外部点击事件来调用

		public function get catBody():CircleParticle
		{
			return _catBody;
		}

		public function set catBody(value:CircleParticle):void
		{
			_catBody = value;
		}

		public function get catSpeed():Number
		{
			return _catSpeed;
		}

		public function set catSpeed(value:Number):void
		{
			_catSpeed = value;
		}

		public function get status():int
		{
			return _status;
		}

		public function set status(value:int):void
		{
			_status = value;
		}

		public function get powered():Boolean
		{
			return _powered;
		}

		public function set powered(value:Boolean):void
		{
			_powered = value;
		}

		public function toggleForce():void {
			_powered = !_powered;
		}
		
		public function addUpForce():void {
			_catBody.addForce(new VectorForce(false,0,-36));
		}
		
		public function addGravity():void {
			_catBody.addForce(new VectorForce(false,0,12));
		}
		
		public function handleCrash():void {
			_status = Cat.STATUS_CRASH;
			_catDisplay.setStatus(Cat.STATUS_CRASH);
			_powered = false;
			// 撞击后 弹力值变为0.3
			_catBody.elasticity = 0.3;
			trace("speed before hit:"+_catBody.velocity);
			var nsp:APEVector = _catBody.velocity;
			// 小猫撞击后的速度其实是固定值，和本身速度没有关系
			_catBody.velocity = nsp.plus( new APEVector(CatGroup.CRASH_HORIZONTAL_SPEED,0));
			trace("hit!  speed:"+_catBody.velocity);
		}
		
		public function printSpeed():void {
			trace("current cat speed:"+_catBody.velocity);
		}
		
	}
}